//
//  SettingViewController.m
//  HandyDialer
//
//	Developed by Team Synergy
//  Created by Jordan Tseng on 11/14/11.
//  Coders: Jacky Li and Jordan Tseng 
//  Graphic Designer: Derek Fong
//  Developers: Brad Lisson, Derek Fong, Jacky Li, Jeremy Gangoso, Jordan Tseng
//  Copyright HandyDialer 2011. All rights reserved.

#import "SettingViewController.h"

@implementation SettingViewController
@synthesize currentSettings;
@synthesize highlightArea;
@synthesize backgroundArea;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}



#pragma mark - View lifecycle


- (void)dealloc {
	[highlightArea release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    counter = 0;
    defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	highlightColor= [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_settings_default.png"]]];
	currentSettings.text = [NSString stringWithFormat:@"Highlight Speed: %@sec(s)\nHighlight Color: %@\nTheme: %@",[[NSString stringWithFormat:@"%f", speed] substringToIndex:3],[highlightColor capitalizedString],[themeColor capitalizedString]];
	[super viewDidLoad];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	highlightColor= [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_settings_default.png"]]];
	currentSettings.text = [NSString stringWithFormat:@"Highlight Speed: %@sec(s)\nHighlight Color: %@\nTheme: %@",[[NSString stringWithFormat:@"%f", speed] substringToIndex:3],[highlightColor capitalizedString],[themeColor capitalizedString]];	if (uniTimer == nil)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(optionChange) userInfo:nil repeats:YES];
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[uniTimer invalidate];
	uniTimer = nil;
	[super viewDidDisappear:animated];
}

- (IBAction)switchView:(id)sender{
	// speed setting
    if(counter == 1)
	{
		counter = 0;
		SettingSpeedViewController	*myViewController = [[SettingSpeedViewController alloc]
                                                     initWithNibName:@"SettingSpeedView"
                                                     bundle:nil];
		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
	}
	// highlight color
	else if(counter == 2)
	{
		counter = 0;
		SettingHighlightColorViewController	*myViewController = [[SettingHighlightColorViewController alloc]
														 initWithNibName:@"SettingHighlightColorView"
														 bundle:nil];
		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
    }
	// theme
    else if(counter == 3)
    {
		counter = 0;
		SettingThemeViewController	*myViewController = [[SettingThemeViewController alloc]
																 initWithNibName:@"SettingThemeView"
																 bundle:nil];
		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
    }
	// about screen
    else if(counter == 4)
    {
        counter = 0;
		SettingAboutViewController	*myViewController = [[SettingAboutViewController alloc]
                                                         initWithNibName:@"SettingAboutView"
                                                         bundle:nil];
		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
    }
    // Go back to main menu
    if (counter == 5)
    {
        counter = 0;
        [self dismissModalViewControllerAnimated:NO];
    }
}

- (void)highlightChange:(NSString *)picture
{
    UIImage *image = [UIImage imageNamed: picture];
    [highlightArea setImage:image];
	
}

// Decides which screen to go to
-(void)optionChange 
{
    
    if (counter == 5)
    {
        counter = 0;
    }
    counter += 1;
    NSString *next = highlightColor;
	next = [next stringByAppendingString:@"_settings_"];  
	next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", counter]];
	next = [next stringByAppendingString:@".png"];
	[self highlightChange:next];
}
@end
